package com.zou.recruit.controller;

import com.zou.recruit.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: 邹祥发
 * @date: 2021/9/20 08:53
 */
@Controller
public class LoginController {

    @Resource
    private UserMapper userMapper;

    //跳转登录
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String user_login() {
        return "user/login";
    }

    //退出登录
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String user_logout(HttpSession session) {
        //清除session
        session.invalidate();

        return "user/login";
    }

    //跳转注册
    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String user_register() {
        return "user/register";
    }

    //登录验证
    @RequestMapping(value = "check", method = RequestMethod.POST)
    public String user_check(HttpServletRequest request, HttpSession session, Model model) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        if (userMapper.selectName(username) != null) {
            //用户名存在
            if (password.equals(userMapper.selectPwdByName(username))) {
                //密码正确
                if (role.equals("1")) {
                    //求职者
                    session.setAttribute("user", username);
                    return "index";
                } else {
                    //企业
                    return "error";
                }
            } else {
                //密码不正确
                model.addAttribute("msg", "密码错误！");
                return "user/login";
            }
        } else {
            //用户名不存在
            model.addAttribute("msg", "用户名不存在！");
            return "user/login";
        }
    }

}
